package org.amphiprion.rfreplayanalyser.ui.progress;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;

import org.amphiprion.rfreplayanalyser.ui.ErrorDialog;

@SuppressWarnings("serial")
public class ProgressDialog extends JDialog {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.amphiprion.rfreplayanalyser.ui.progress.messages"); //$NON-NLS-1$

	private final JPanel contentPanel = new JPanel();
	private volatile boolean succeed = false;
	private JProgressBar progressBar = new JProgressBar(0, 100);
	private JLabel lblMessage = new JLabel();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ProgressDialog dialog = new ProgressDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ProgressDialog() {
		setTitle(BUNDLE.getString("ProgressDialog.this.title")); //$NON-NLS-1$
		setBounds(100, 100, 450, 121);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			contentPanel.add(lblMessage, BorderLayout.NORTH);
		}
		{

			contentPanel.add(progressBar, BorderLayout.CENTER);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public static boolean launchWork(Component parent, final ProgressTask task) {
		final ProgressDialog dlg = new ProgressDialog();
		dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

		dlg.setModal(true);

		dlg.setLocationRelativeTo(parent);

		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				task.run(new ProgressHandler() {

					@Override
					public void progress(int pct, String message) {
						dlg.progressBar.setValue(pct);
						dlg.lblMessage.setText(message);
					}

					@Override
					public void errorOccurs(Throwable t) {
						ErrorDialog.showError(t, dlg);
						dlg.succeed = false;
						dlg.setVisible(false);
						dlg.dispose();
					}

					@Override
					public void taskEnded() {
						dlg.progressBar.setValue(100);
						dlg.succeed = true;
						dlg.setVisible(false);
						dlg.dispose();
					}

				});
			}
		});
		thread.setName(task.getName());
		thread.start();

		dlg.setVisible(true);
		return dlg.succeed;
	}
}
